<?php

/**
 * 海风商城 商品页
 * 
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

$act = !empty($_GET['act']) ? $_GET['act'] : '';
$goods_id = !empty($_GET['id']) ? intval($_GET['id']) : '';
$from = !empty($_GET['from']) ? $_GET['from'] : '';

$_LANG['kilogram'] = '千克';
$_LANG['gram'] = '克';
$_LANG['home'] = '首页';
$_LANG['goods_attr'] = '';

$is_vip = !empty($_GET['vip']) ? $_GET['vip'] : '';

$cats=array();
if(!empty($_GET['cat']))
{
    $cats=explode('-',$_GET['cat']);
}


$attr_info = '';
switch ( $cats[0] ) {
    case 0:
        $cat0 = 0;
        $keywords = '初中';
        break;
    case 1:
        $cat0 = 1;
        $keywords = '高中';
        break;
    case 2:
        $cat0 = 2;
        $keywords = '升学';
        break;
    default:
        $cat0 = 0;
        $keywords = '初中';
        break;
}


if( $course_price[$cat0][0] < $course_price[$cat0][1] ) $has_max_price = 1;
$min_price = $course_price[$cat0][0];


//获取学习方案
$sql="select * from ".$ecs->table('category')." category where parent_id=0 and keywords like '%".$keywords."%'";
$cat_info=$db->getAll($sql);

if( !$cat_info ){
    if( isset($_SERVER['HTTP_REFERER']) ) ecs_header("Location: ".$_SERVER['HTTP_REFERER']."\n");
    else ecs_header("Location: /\n");
    exit;
}

//获取年级
$cat1 = $cats[1]?$cats[1]:$cat_info[0]['cat_id'];

$sub_sql="select * from ".$ecs->table('category')." category where parent_id=".$cat1." and keywords like '%".$keywords."%'";
$sub_cats=$db->getAll($sub_sql);

if( !$sub_cats ){
    $cat1 = $cat_info[0]['cat_id'];
    $sub_sql="select * from ".$ecs->table('category')." category where parent_id=".$cat1." and keywords like '%".$keywords."%'";
    $sub_cats=$db->getAll($sub_sql);
}

//获取课时  
$cat2 = $cats[2]?$cats[2]:$sub_cats[0]['cat_id'];
if( $is_vip ){
    $sql="select * from ".$ecs->table('goods')." goods where cat_id='".$is_vip."'";
    $goods_info=$db->getAll($sql);
    $sql="select * from ".$ecs->table('goods_attr')." goods_attr where goods_id='".$goods_info[0]['goods_id']."'";
    $goods_temp_attr=$db->getAll($sql); 
}else{
    $sql="select * from ".$ecs->table('goods')." goods where cat_id='".$cat2."'";
    $goods_info=$db->getAll($sql);
    $sql="select * from ".$ecs->table('goods_attr')." goods_attr where goods_id='".$goods_info[0]['goods_id']."'";
    $goods_temp_attr=$db->getAll($sql);
    if ( !$goods_temp_attr && $goods_info ) {
        $only_price = 1;
        $min_price = $goods_info[0]['market_price'];
    }
}


//年级处理
foreach ($sub_cats as $key => &$value) {
    if(mb_strpos($value['cat_name'],'星')){
        if( $is_vip && $is_vip == $value['cat_id'] ) $vip_data[0]['curr'] = 'curr';
        $vip_data[0]['cat_name'] = "五星名师";


        if ($is_vip) $vip_data[0]['link'] = '?cat='.$cat0.'-'.$cat1.'-'.$cats[2].'-'.$cats[3];
        else $vip_data[0]['link'] = '?cat='.$cat0.'-'.$cat1.'-'.$cats[2].'-'.$cats[3].'&vip='.$value['cat_id'];
        unset($sub_cats[$key]);
        continue;
    }

    if( $value['cat_id'] == $cats[2] ) {
        $value['curr'] = 'curr';
        $has_curr1 = 1;
        $cur_class = $value['cat_name'];
    }

    $value['link'] = '?cat='.$cat0.'-'.$cat1.'-'.$value['cat_id'].'-'.$cats[3];
    if( $is_vip ) $value['link'].= '&vip='.$is_vip;

}


//学习方案 处理
foreach ($cat_info as $key => &$value) {
    if( $value['cat_id'] == $cats[1] ) {
        $value['curr'] = 'curr';
        $has_curr0 = 1;
        $value['link'] = '?cat='.$cat0.'-'.$value['cat_id'].'-'.$cats[2];
    }else{
        if( $cur_class ){
            $cat_id_sql="select cat_id from ".$ecs->table('category')." category where parent_id=".$value['cat_id']." and cat_name like '%".$cur_class."%'";
            $cat_id=$db->getOne($cat_id_sql);
            $value['link'] = '?cat='.$cat0.'-'.$value['cat_id'].'-'.$cat_id;
        }else $value['link'] = '?cat='.$cat0.'-'.$value['cat_id'].'-'.$cats[2];        
    }
    
    if( $cats[3] ) $value['link'] .= '-'.$cats[3];
}
$attr_info['学习方案'] = $cat_info;

$attr_info['年级'] = $sub_cats;

//课时处理
if( $goods_temp_attr ){
    foreach ($goods_temp_attr as $k => $v) {

        if( $cats[3] && $cats[3] == $v['attr_value'] ) {
            $min_price = $v['attr_price'];
            $has_curr2 = 1;
            $data[$k]['curr'] = 'curr';
            $has_max_price = 0;
            $goods_id = $v['goods_id'];
            $attr_id = $v['goods_attr_id'];
        }

        $data[$k]['cat_name'] = $v['attr_value'];
        $data[$k]['cat_price'] = $v['attr_price'];
        $data[$k]['cat_id'] = $v['attr_id'];
        $data[$k]['goods_id'] = $v['goods_id'];
        $data[$k]['goods_attr_id'] = $v['goods_attr_id'];

        $data[$k]['link'] = '?cat='.$cat0.'-'.$cat1.'-'.$cats[2].'-'.$v['attr_value'];
        if( $is_vip ) $data[$k]['link'].= '&vip='.$is_vip; 

    }
    $attr_info['课时'] =  $data;
}

//是否VIP
if( $vip_data ) $attr_info['VIP'] = $vip_data;


$smarty->assign('fangan', $cat1);
$smarty->assign('nianji', $cats[2]);
$smarty->assign('keshi',  $cats[3]);
$smarty->assign('is_vip', $is_vip);
$smarty->assign('goods_id', $goods_id);
$smarty->assign('attr_id', $attr_id);
$smarty->assign('only_price', $only_price);
$smarty->assign('min_price', $min_price);
$smarty->assign('has_max_price', $has_max_price);

if( $has_max_price ) $smarty->assign('max_price', $course_price[$cat0][1]);
$smarty->assign('specification', $attr_info);
$smarty->assign('goods_info', $goods_info);

$is_login = ($_SESSION['user_id'] > 0)? 1 : 0;
$smarty->assign('is_login', $is_login);
$smarty->display('goods.html');


?>